package com.xtm.exercise.utils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.regex.Pattern;

public final class DataCheckUtil {

	public static boolean checkDriverName(String name) {

		return Pattern.matches("(^([a-z]|[A-Z]|[\\u4e00-\\u9fa5]|[^\\x00-\\xff]){1,20}$)", name);
	}

	public static boolean checkYesNo(String flg) {
		if (StringUtils.isEmpty(flg)) {
			flg = "否";
		}

		return flg.equals("否") || flg.equals("是");
	}

	/**
	 * 判断性别（男/女） CheckFormatUtil
	 * 男 或 null =true / 女 = false
	 */
	public static boolean checkSex(String sex) {
		if (StringUtils.isEmpty(sex)) {
			sex = "男";
		}

		return sex.equals("男") || sex.equals("女");
	}

	public static boolean isNumber(String str) {

		return StringUtils.isNotEmpty(str) && Pattern.matches("(^([0-9]+(\\.[0-9]+)?)$)", str);
	}

	/*
	 * 判断是否是手机号 1开头的11位数字返回true 除此以外判断为false
	 */
	public static boolean checkPhoneNumber(String phone) {
		return Pattern.matches("(^1[0-9]{10}$)", phone);
	}

	public static boolean checkIdentityNumber(String id) {
		return Pattern.matches(
				"(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)",
				id);
	}

	public static boolean checkShortNumber(String number) {
		return Pattern.matches("(^[0-9]{1,8}$)", number);
	}

	public static Timestamp changeToTimestamp(String str) {

		try {
			Date date = DateTimeUtil.strToDate(str, "M/d/yy HH:mm");
			return new Timestamp(date.getTime());
		} catch (Exception e) {
			return null;
		}

	}
	
	
	public static boolean checkVehicleNumber(String str) {
		                         
		return Pattern.matches("(^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{6})|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$)", str);
	}
	
	public static boolean checkFrameNumber(String str) {

		return Pattern.matches("(^([a-zA-Z0-9]){0,17}$)", str);
	}
	
	public static boolean checkEngineNumber(String str) {
		return Pattern.matches("(^[A-Z0-9]{0,32}$)", str);
	}
	
	public static boolean checkDrivingPermitNo(String str) {
		return Pattern.matches("(^[0-9]{0,12}$)", str);
	}
	public static boolean checkFixedNumbers(String str) {
		return Pattern.matches("(^[0-9]{0,3}$)", str);
	}
	
	public static boolean checkOutterName(String str) {
		return Pattern.matches("(^([\\u4e00-\\u9fa5]|[\\uFE30-\\uFFA0]|[a-z]|[A-Z]|[0-9]){0,60}$)", str);
	}
	
	public static boolean checkAssetsCard(String str) {
		//return Pattern.matches("(^[0-9]{0,18}$)", str);
		return Pattern.matches("(^[a-zA-Z0-9-]{0,24}$)", str);
	}
	
	public static boolean checkName(String name) {
		return Pattern.matches("(^([a-z]|[A-Z]|[0-9]|[\\u4e00-\\u9FFF]|[\\uFE30-\\uFFA0]){0,20}$)", name);
	}
	
	public static boolean checkOfficailPost(String name) {
		return Pattern.matches("(^([a-z]|[A-Z]|[0-9]|[\\u4e00-\\u9FFF]|[\\uFE30-\\uFFA0]){0,25}$)", name);
	}
	//1-99数字
	//两位正整数
	public static boolean checkUseNumber(String name) {
        return Pattern.matches("(^([1-9]|[1-9]\\d{1})$)", name);
    }
	//四位正整数+'.'+二位小数
	public static boolean checkFourDigitNumber(String name){
	    return Pattern.matches("(^\\d{1,4}(\\.[0-9]{1,2})?$)", name);
	}
	//校验字符长度
	public static boolean checkLength(String name,int maxLen) {
        return Pattern.matches("^.{0," + maxLen + "}$", name);
    }
	//六位整数+'.'+二位小数
	//999999.99的数字 小数点后1-到2位
	public static boolean checkMoney(String name){
	    return Pattern.matches("(^\\d{1,6}(\\.[0-9]{1,2})?$)", name);
	}
	//二位整数+'.'+二位小数
	public static boolean checkTwoDigitNumber(String name){
	    return Pattern.matches("(^\\d{1,2}(\\.[0-9]{1,2})?$)", name);
	}
	
	/**
	 * 校验字符
	 * @param name
	 * @param maxLen
	 * @return
	 */
	public static boolean specialTextLength(String name,int maxLen){
	    //不包含这些字符 返回true
	    boolean specialTextFlag =  Pattern.matches("^[^<>%;/\\+#$！？?}{【】《》~`\\|!=￥&*\"':@]{0,1000}", name);
	    //长度在最大长度返回内返回true
	    boolean lengthFlag = checkLength(name, maxLen);
	    return specialTextFlag && lengthFlag;
	}
}
